package com.qiku.controller;


import com.github.pagehelper.PageInfo;
import com.qiku.domain.ResponseResult;
import com.qiku.domain.Role;
import com.qiku.domain.User;
import com.qiku.domain.UserVo;
import com.qiku.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页获取用户数据&条件查询用户数据
     * @param userVo
     * @return
     */
    @PostMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage( @RequestBody UserVo userVo){
        PageInfo<User> pageInfo = userService.findAllUserByPage(userVo);
        ResponseResult result = new ResponseResult(true, 200, "响应成功~", pageInfo);
        return result;
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(@RequestParam Integer id, @RequestParam String status){
        ResponseResult result = null;
        try{
            userService.updateUserStatus(id,status) ;
            Map<String, String> map = new HashMap<>();
            map.put("status",status);
            result = new ResponseResult(true, 200, "响应成功~", map);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", null);
            return result;
        }
    }

    @PostMapping("/login")
    public ResponseResult login(UserVo userVo , HttpServletRequest request){
        try {
            User login = userService.login(userVo);
            ResponseResult result = null;
            if (null != login){
                //密码正确
                HashMap<String, Object> map = new HashMap<>();
                String access_token = UUID.randomUUID().toString();
                map.put("access_token",access_token);
                map.put("user_id",login.getId());
                //获取session对象
                HttpSession session = request.getSession();
                //保存用户登录状态
                session.setAttribute("user_id",login.getId());
                session.setAttribute("access_token",access_token);
                result = new ResponseResult(true, 1, "密码正确~", map);
                return result;
            } else {
                //密码错误
                result = new ResponseResult(false, 201, "用户名或者密码错误~", null);
                return result;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 查询当前用户对应的角色信息
     * @param id
     * @return
     */
    @GetMapping("/findUserRoleById")
    public ResponseResult findUserRelationRoleById(Integer id){
        List<Role> roleList = userService.findUserRelationRoleById(id);
        ResponseResult result = null;
        if (null != roleList){
            result = new ResponseResult(true, 200, "角色回显成功~", roleList);
        }
        return result;
    }


    /**
     * 分配角色
     * @param userVo
     * @return
     */
    @PostMapping("/userContentRole")
    public ResponseResult userContentRole(@RequestBody UserVo userVo){
        userService.userContentRole(userVo);
        ResponseResult result = new ResponseResult(true,200,"角色分配成功~",null);
        return result;
    }


    /**
     * 获取用户权限
     * @return
     */
    @GetMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request){
        //获取请求头中的token
        String token = request.getHeader("Authorization");
        //获取session中的access_token
        String access_token = (String) request.getSession().getAttribute("access_token");
        if (token.equals(access_token)){
            //从session中获取user_id
            Integer user_id = (Integer) request.getSession().getAttribute("user_id");
            ResponseResult result = userService.getUserPermissions(user_id);
            return result;
        } else {
            return new ResponseResult(false,500,"获取失败~",null);
        }

    }
}
